/*
 * bmp084.c
 *
 * Created: 23.12.2013 08:17:00
 *  Author: Cedomir
 */ 

#define F_CPU 12000000UL

#include <avr/io.h>
#include <util/delay.h>
#include "i2c_master.h"
#include <stdio.h>
#include <string.h>
#include "defs.h"

// Adressen von I2C-Bausteinen
#define DevEAT123A	 0x74 // LCD EAT123A,	(0 1 1 1 0  1 SA0 R/W)
#define BMP085_WRITE 0xEE
#define BMP085_READ  0xEF
#define OSS			 0
#define TOUTH        0x02
#define BOTH         0x12



void bmp085_Calibration(void);

short bmp085_ReadCal(unsigned char address);
long  bmp085_ReadTemp(void);
long  bmp085_ReadPres(void);
void  bmp085_Convert(long * temperature, long * pressure);



void LCD_init(void);
void DS1621_init(void);
int display_outstring(char *outtext);


char temp_string[12];
char press_string[12];
char alt_string[12];
unsigned int result;
float wert;
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;

char outstring_ac1[12], outstring_ac2[12], outstring_ac3[12], outstring_ac4[12],
     outstring_ac4[12], outstring_ac5[12], outstring_ac6[12], outstring_b1[12],
	 outstring_b2[12], outstring_mb[12], outstring_mc[12], outstring_md[12];

long temperature=0;

int main(void)
{
	 long temperature = 0;
	 long pressure = 0;
	 long altitude = 0;
	 double temp = 0;
	 
	 
	 DDRC  |=  0xFC;          		// use all pins on port C for output
	 PORTC &= ~0xFC;          		// (LEDs on /twi not aktiv)


	 i2c_init ();                  		// init I2C interface
	 LCD_init();						// Initialisierung LC Display EAT123A-I2C
	 
	 bmp085_Calibration();
	
    while(1)
    {
       
	   bmp085_Convert(&temperature,&pressure);
	   
	   temperature = temperature*0.1;
	   
	   sprintf(temp_string," %ld Grad ",temperature);
	   sprintf(press_string,"%ld Pa",pressure);
	   
	   temp = (double) pressure/101325;
	   temp = 1-pow(temp, 0.19029);
	   altitude = round(44330*temp);
	   sprintf(alt_string, "%ldm", altitude);
	   
	   i2c_start(0x74);
	   i2c_write(0x00);
	   i2c_write(0xA0);
	   display_outstring(alt_string);
	   
	   
	   _delay_ms(1000);
	   
		
    }
}

void bmp085_Calibration(void)
{
	
	ac1 = bmp085_ReadCal(0xAA);
	ac2 = bmp085_ReadCal(0xAC);
	ac3 = bmp085_ReadCal(0xAE);
	ac4 = bmp085_ReadCal(0xB0);
	ac5 = bmp085_ReadCal(0xB2);
	ac6 = bmp085_ReadCal(0xB4);
	b1  = bmp085_ReadCal(0xB6);
	b2  = bmp085_ReadCal(0xB8);
	mb  = bmp085_ReadCal(0xBA);
	mc  = bmp085_ReadCal(0xBC);
	md  = bmp085_ReadCal(0xBE);
	
	
	sprintf(outstring_ac1,"ac1 = %d",ac1);
	sprintf(outstring_ac2,"ac2 = %d",ac2);
	sprintf(outstring_ac3,"ac3 = %d",ac3);
	sprintf(outstring_ac4,"ac4 = %d",ac4);
	sprintf(outstring_ac5,"ac5 = %d",ac5);
	sprintf(outstring_ac6,"ac6 = %d",ac6);
	sprintf(outstring_b1,"b1 = %d",b1);
	sprintf(outstring_b2,"b2 = %d",b2);
	sprintf(outstring_mb,"mb = %d",mb);
	sprintf(outstring_mc,"mc = %d",mc);
	sprintf(outstring_md,"md = %d",md);
	
	/*
	   i2c_start(0x74);
	   i2c_write(0x00);
	   i2c_write(0xA0);
	   display_outstring(outstring_mc);
	  */ 
		
}

short bmp085_ReadCal(unsigned char address)
{
	
	short result;
	
	i2c_start(BMP085_WRITE);
	i2c_write(address);
	i2c_stop();
	
	i2c_start(BMP085_READ);
	result=i2c_readAck()<<8;
	result|=i2c_readNak();
	i2c_stop();
	
	return result;
	
}


long bmp085_ReadTemp(void)
{
	
	long temperature = 0;
	
	i2c_start(BMP085_WRITE);
	i2c_write(0xF4);
	i2c_write(0x2E);
	i2c_stop();
	_delay_ms(10);
	
	temperature = bmp085_ReadCal(0xF6);
	
	return temperature;

}


long bmp085_ReadPres(void)
{
	
	long pressure = 0;
	
	i2c_start(BMP085_WRITE);
	i2c_write(0xF4);
	i2c_write(0x34);
	i2c_stop();
	_delay_ms(10);
	
	pressure = bmp085_ReadCal(0xF6);
	
	pressure &= 0x0000FFFF;
	
	return pressure;

}


void bmp085_Convert(long * temperature, long * pressure)
{
	
	//Temperatur Berechnung
	
	long ut, up;
	long x1, x2, b5;
	
	ut = bmp085_ReadTemp();
	ut = bmp085_ReadTemp();
	up = bmp085_ReadPres();
	up = bmp085_ReadPres();	
	
	x1 = ((long) ut -ac6) * ac5 >> 15;
	x2 = ((long) mc << 11) / (x1 + md);
	b5 = x1 + x2;
	*temperature = (b5 + 8) >> 4;


	//Druck Berechnung
	
	long b6, x3, b3, p;
	unsigned long b4, b7;
	
	b6 = b5 - 4000;
	x1 = (b2 * (b6 * b6 >> 12)) >> 11;
	x2 = ac2 * b6 >> 11;
	x3 = x1 + x2;
	b3 = (((int32_t) ac1 * 4 + x3) + 2)/4;
	x1 = ac3 * b6 >> 13;
	x2 = (b1 * (b6 *b6 >> 12)) >> 16;
	x3 = ((x1 + x2) + 2) >> 2;
	b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
	b7 = ((unsigned long) up - b3) * (50000 >> OSS);
	p = b7 < 0x80000000 ? (b7 * 2) / b4 : (b7 / b4) * 2;
	x1 = (p >> 8) * (p >> 8);
	x1 = (-7357 * p) >> 16;
	*pressure = p + ((x1 + x2 + 3791) >> 4);
}


int display_outstring(char *outtext)
{
	
	unsigned char length = strlen(outtext);
	unsigned char count;
	unsigned char count2;

	i2c_start(0x74);
	_delay_ms (1.0);
	i2c_write(0x40);

	if (length>12)
	length+=6;
	if (length>=24)
	length+=6;

	for (count=0; count < length; count++)
	{
		if (count<12)
		{
			count2=count;
			i2c_write(outtext[count2] +0x80);
			_delay_ms (1.0);
		}
	}
	i2c_stop();
	return 0;
	
}

void LCD_init(void)					/* Initialisierung LCD */
{
	
	i2c_start(0x74);			// Start with Slave- Adress
	_delay_ms (1.0);
	i2c_write(0x00);			// Send Control Byte
	_delay_ms (1.0);
	i2c_write(0x2E);			// Funktion Set
	_delay_ms (1.0);
	i2c_write(0x01);			// Clear Display
	_delay_ms (1.0);
	i2c_write(0x0E);			// Display on/off control
	_delay_ms (1.0);
	i2c_write(0x06);			// 	Entry mode set
	
	//i2c_stop();
	
}